<?php

namespace App\Admin\Controllers\Scaffold;

use App\Admin\Extensions\Scaffold\ControllerCreator;
use App\Admin\Extensions\Scaffold\DBMapping;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class ControllerGeneratorController extends Controller
{
    const fieldTypes = [
                //Numeric
                'BIGINT', 'MEDIUMINT', 'INT', 'SMALLINT', 'TINYINT',
                'DECIMAL', 'NUMERIC',
                'FLOAT', 'DOUBLE', 'REAL',
                'BIT',
                //String
                'VARCHAR', 'CHAR',
                'BINARY', 'VARBINARY',
                'LONGBLOB', 'MEDIUMBLOB', 'BLOB', 'TINYBLOB',
                'LONGTEXT', 'MEDIUMTEXT', 'TEXT', 'TINYTEXT',
                'ENUM',
                'SET',
                //Datetime
                'DATE', 'DATETIME',
                'TIMESTAMP',
            ];

    const controlTypes = [
                'n/a',
                'hidden',
                'text',
                'select',
                'multipleSelect',
                'listbox',
                'textarea',
                'radio',
                'checkbox',
                'email',
                'password',
                'url',
                'ip',
                'mobile',
                'color',
                'date',
                'datetime',
                'timeRange',
                'dateRange',
                'datetimeRange',
                'currency',
                'number',
                'rate',
                'image',
                'file',
                'multipleImage',
                'multipleFile',
                'editor',
                'switch',
                'display',
                'html'
            ];

    /**
     * index page
     *
     * @return mixed
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Controller Generator');

            $tableNames = DBMapping::getTables();

            $content->row(view('admin.scaffold.controller_generator',
                               [
                                   'tableNames' => $tableNames,
                                   'fieldTypes' => self::fieldTypes,
                                   'controlTypes' => self::controlTypes,
                                   'code' => ""
                               ]));
        });
    }

    /**
     * generate code
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function generate(Request $request)
    {
        try {

            // get params
            $tableComment   = $request->get('table_comment');
            $controllerName = $request->get('controller_name');
            $modelName      = $request->get('model_name');
            $fields         = $request->get('fields');

            $content = (new ControllerCreator($tableComment, $controllerName, $modelName, $fields))->create();

        } catch (\Exception $exception) {

            $bundle = [
                'result' => 'failed',
                'code' => $exception->getTraceAsString(),
            ];

            return response()->json($bundle);
        }

        $bundle = [
            'result' => 'success',
            'code' => $content,
        ];

        return response()->json($bundle);
    }

    /**
     * get table schema by ajax
     *吃
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function schema(Request $request)
    {
        $tableName = $request->input("table_name");

        $fields = DBMapping::getFields($tableName);
        $tableComment = DBMapping::getTableComment($tableName);
        $modelName = "App\\Biz\\Model\\".ucfirst(camel_case($tableName));
        $controllerName = "App\\Admin\\Controllers\\".ucfirst(camel_case($tableName))."Controller";

        $bundle = [
            'fields' => $fields,
            'table_comment' => $tableComment,
            'model_name' => $modelName,
            'controller_name' => $controllerName
        ];

        return response()->json($bundle);
    }
}
